import { takeEvery, put, fork, delay, take, cancel } from 'redux-saga/effects'
import { actionTypes, increase, decrease } from '../action/counter'

function* conterTask() {
    console.log('conterTask');
    yield put(increase())
}

function* fockTask() {
    let task
    while (true) {
        yield take(actionTypes.asyncDecrease)
        // 监听到了action,并且在开启新任务之前，取消之前的任务
        if (task) yield cancel(task)
        task = yield fork(function* () {
            yield delay(2000)
            yield put(decrease())
            console.log('fockTask');
        })
    }

}

export default function* () {
    yield fork(fockTask)
    yield takeEvery(actionTypes.asyncDecrease, conterTask)
}